package com.tianyang.yukuaifu.business.trade.view;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.adapter.ServicePlatformAdapter;
import com.tianyang.yukuaifu.business.trade.bean.ServicePlatformOutput;
import com.tianyang.yukuaifu.business.trade.contract.ServicePlatformContract;
import com.tianyang.yukuaifu.business.trade.presenter.ServicePlatformPresenter;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description：服务商平台
 * Created by xyj on 2024/3/25
 */
public class ServicePlatformActivity extends BaseTitleActivity implements ServicePlatformContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private ServicePlatformHeaderView vHeaderView;

    private RelativeLayout emptyView;

    private ServicePlatformAdapter vServicePlatformAdapter;

    private int nowPage = CommonConfig.PAGE_NUM;

    private ServicePlatformContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("服务商平台");

        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vServicePlatformAdapter = new ServicePlatformAdapter();
        vHeaderView = new ServicePlatformHeaderView(mContext, null);
        emptyView = vHeaderView.findViewById(R.id.emptyView);
        vServicePlatformAdapter.addHeaderView(vHeaderView);
        vRecyclerview.setAdapter(vServicePlatformAdapter);
        vServicePlatformAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vServicePlatformAdapter.setPreLoadNumber(2);

        mPresenter = new ServicePlatformPresenter(this);
        mPresenter.appAgentHomeStatQb(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.appAgentHomeStatQb(nowPage);
                vServicePlatformAdapter.setEnableLoadMore(true);
            }
        });
        vServicePlatformAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.appAgentHomeStatQb(nowPage);
            }
        }, vRecyclerview);
    }

    @Override
    public void showAgentHomeStatView(JSONObject jsonObject) {
        vHeaderView.showServicePlatformView(jsonObject);
    }

    @Override
    public void showAgentHomeListView(List<ServicePlatformOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vServicePlatformAdapter.setNewData(outputs);
            } else {
                vServicePlatformAdapter.addData(outputs);
            }
            nowPage++;
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vServicePlatformAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vServicePlatformAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vServicePlatformAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vServicePlatformAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ServicePlatformContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
